import java.util.List;
import java.util.Scanner;

public class TicketService {
    private Scanner scanner;
    private TicketDAO ticketDAO;
    private ScheduleDAO scheduleDAO;
    private MovieDAO movieDAO;

    public TicketService(Scanner scanner) {
        this.scanner = scanner;
        this.ticketDAO = new TicketDAO();
        this.scheduleDAO = new ScheduleDAO();
        this.movieDAO = new MovieDAO();
    }

    // 查看用户电影票
    public void listUserTickets(String username) {
        List<Ticket> tickets = ticketDAO.findTicketsByUsername(username);
        if (tickets.isEmpty()) {
            System.out.println("你暂无已购买的电影票！");
            return;
        }

        System.out.println("\n=== 你的电影票列表 ===");
        System.out.println("ID\t场次ID\t电影ID\t座位号\t价格\t购买时间");
        System.out.println("----------------------------------------");
        for (Ticket ticket : tickets) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%.2f\t%s",
                    ticket.getId(), ticket.getScheduleId(), ticket.getMovieId(),
                    ticket.getSeatNumber(), ticket.getPrice(), ticket.getPurchaseTime()));
        }
    }

    // 购买电影票
    public boolean buyTicket(String username) {
        System.out.print("请输入要购买的场次ID: ");
        String scheduleId = scanner.nextLine();

        Schedule schedule = scheduleDAO.findScheduleById(scheduleId);
        if (schedule == null) {
            System.out.println("该场次不存在！");
            return false;
        }

        if (schedule.getRemainingSeats() <= 0) {
            System.out.println("该场次已无剩余座位，无法购买！");
            return false;
        }

        System.out.print("请输入座位号: ");
        String seatNumber = scanner.nextLine();
        // 简单校验座位号格式
        if (!seatNumber.matches("^[A-Z]\\d+$")) {
            System.out.println("座位号格式不正确，例如 A1、B2 等，请重新输入！");
            return false;
        }

        // 检查该座位是否已被购买
        if (ticketDAO.isSeatBooked(scheduleId, seatNumber)) {
            System.out.println("该座位已被购买，请选择其他座位！");
            return false;
        }

        // 获取电影票价
        Movie movie = movieDAO.findMovieById(schedule.getMovieId());
        if (movie == null) {
            System.out.println("关联电影信息不存在！");
            return false;
        }

        // 生成票ID
        String ticketId = "T" + System.currentTimeMillis();
        double price = movie.getPrice();
        String movieId = schedule.getMovieId();
        String purchaseTime = java.time.LocalDateTime.now().toString();

        Ticket ticket = new Ticket(ticketId, scheduleId, movieId, seatNumber, price, username, purchaseTime);
        boolean result = ticketDAO.addTicket(ticket);
        if (result) {
            // 购买成功后，更新场次剩余座位数
            scheduleDAO.updateRemainingSeats(scheduleId, schedule.getRemainingSeats() - 1);
            System.out.println("购票成功！");
        } else {
            System.out.println("购票失败，请稍后重试！");
        }
        return result;
    }

    // 退票
    public boolean refundTicket(String username) {
        System.out.print("请输入要退的票ID: ");
        String ticketId = scanner.nextLine();

        Ticket ticket = ticketDAO.findTicketById(ticketId);
        if (ticket == null) {
            System.out.println("该电影票不存在！");
            return false;
        }

        if (!ticket.getUsername().equals(username)) {
            System.out.println("你无权退该电影票！");
            return false;
        }

        boolean result = ticketDAO.deleteTicket(ticketId);
        if (result) {
            // 退票成功，恢复场次剩余座位数
            Schedule schedule = scheduleDAO.findScheduleById(ticket.getScheduleId());
            if (schedule != null) {
                scheduleDAO.updateRemainingSeats(ticket.getScheduleId(), schedule.getRemainingSeats() + 1);
            }
            System.out.println("退票成功！");
        } else {
            System.out.println("退票失败，请稍后重试！");
        }
        return result;
    }

    // 查看所有电影票（管理员、经理等角色可用）
    public void listAllTickets() {
        List<Ticket> tickets = ticketDAO.getAllTickets();
        System.out.println("\n=== 所有电影票列表 ===");
        System.out.println("ID\t场次ID\t电影ID\t座位号\t价格\t用户名\t购买时间");
        System.out.println("----------------------------------------");
        for (Ticket ticket : tickets) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%.2f\t%s\t%s",
                    ticket.getId(), ticket.getScheduleId(), ticket.getMovieId(),
                    ticket.getSeatNumber(), ticket.getPrice(), ticket.getUsername(), ticket.getPurchaseTime()));
        }
    }
}

